package yu.zhou.njupt.fileprocessor;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import oracle.net.aso.f;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import yu.zhou.njupt.fileprocessor.bean.CaseSensitive;
import yu.zhou.njupt.fileprocessor.bean.MatchMode;

/**
 * @author yu.zhou
 * 
 */
@SuppressWarnings("unused")
public class FileCopy2 extends FileProcessor {
    
    private static final Logger logger = LoggerFactory.getLogger(FileCopy2.class);
    
    private String sourceFilePathStr = null;
    private File newFilePath = null;
    private String newFilePathStr = null;

    public FileCopy2() {
    	numOFThread = 1;
    }
    
    public FileCopy2(int threadNum) {
    	this.numOFThread = threadNum;
    }
    
	public int work(File sourcePath, File destinationPath, Map<String, String> filenameMap, MatchMode matchMode, CaseSensitive caseSensitive) throws Exception {
		if (!sourcePath.exists()) {
			throw new Exception("源文件的根目录不存在,请检查!!!");
		} else if (!sourcePath.isDirectory()) {
			throw new Exception("提供的根目录是一个文件,请检查!!!");
		} else {
			numOfProcessFiles = 0;
			newFilePath = destinationPath;
			sourceFilePathStr = sourcePath.getAbsolutePath() + File.separator;
			newFilePathStr = newFilePath.getAbsolutePath() + File.separator;
			if (!newFilePath.exists()) {
				newFilePath.mkdir();
			}
			
			renameFileByDirectory(sourcePath, filenameMap, matchMode, caseSensitive);

			return numOfProcessFiles;
		}
	}
	
	public int work(String sourcePathStr, String destinationPathStr, Map<String, String> filenameMap, MatchMode matchMode, CaseSensitive caseSensitive) throws Exception {
	    return work(new File(sourcePathStr), new File(destinationPathStr), filenameMap, matchMode, caseSensitive);
	}
	
	private void renameFileByDirectory(File sourcePath, Map<String, String> filenameMap, MatchMode mode, CaseSensitive caseSensitive) throws IOException {
	    String directoryName = sourcePath.getName();
	    String targetDirecotry = (sourcePath.getAbsolutePath() + File.separator).replace(sourceFilePathStr, newFilePathStr);
	    int count = 0;

	    for (File file : sourcePath.listFiles()) {
	        if (file.isDirectory()) {
	            File newDir = new File((file.getAbsolutePath() + File.separator).replace(sourceFilePathStr, newFilePathStr));
	            if (!newDir.exists()) {
	                newDir.mkdir();
	            }
	            renameFileByDirectory(file, filenameMap, mode, caseSensitive);
	        } else {
	            count++;
	            numOfProcessFiles++;
	            String sourceFilename = file.getName();
	            String filename = sourceFilename.substring(0, sourceFilename.lastIndexOf("."));
	            String filetype = sourceFilename.substring(sourceFilename.lastIndexOf(".") + 1);
	            String newFilename = directoryName + "_" + String.format("%5s", count).replace(" ", "0")
	                    + "." + filetype; 
	            FileUtils.copyFile(file, new File(targetDirecotry + newFilename));
	        }
	    }
	}
	
}
    
